package com.youdoneed.friends.common.util;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

public class HttpClientUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    //http:post方法 人脸识别调用该方法
    public static String sendHttpPostToFace(String url, String data) throws IOException {
        String output = "";
        try {
            URL targetUrl = new URL(url);
            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConnection.setConnectTimeout(600000);//连接超时 单位毫秒
            httpConnection.setReadTimeout(200000);//读取超时 单位毫秒
            httpConnection.setRequestMethod("POST");

            OutputStreamWriter outputStream = new OutputStreamWriter(httpConnection.getOutputStream(), "UTF-8");

            outputStream.write(data);
            outputStream.flush();
            outputStream.close();
            if (httpConnection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : "
                        + httpConnection.getResponseCode());
                return "false";
            }
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));
            String s = "";
            while ((s = responseBuffer.readLine()) != null) {
                output += s;
            }
            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }

    //http:post方法
    public static String sendHttpPost(String url, Map map, String token) throws IOException {
        String output = "";
        try {
            URL targetUrl = new URL(url);
            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            if (token != null) {
                String temp = "Bearer " + token;
                httpConnection.setRequestProperty("Authorization", temp);
            }
            Gson gson = new Gson();
            String param = gson.toJson(map).toString();
            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(param.getBytes());
            outputStream.flush();
            outputStream.close();
            if (httpConnection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : "
                        + httpConnection.getResponseCode());
                return "false";
            }
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));
            String s = "";
            while ((s = responseBuffer.readLine()) != null) {
                output += s;
            }
            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }

    //http:get方法
    public static String sendHttpGet(String url, String token) throws IOException {
        String output = "";
        try {
            URL restServiceURL = new URL(url);
            HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
            httpConnection.setRequestMethod("GET");
            httpConnection.setRequestProperty("Accept", "application/json");
            if (token != null) {
                String temp = "Bearer " + token;
                httpConnection.setRequestProperty("Authorization", temp);
            }

            if (httpConnection.getResponseCode() != 200) {
                logger.error("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
                return "false";
            }

            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));
            String s;
            while ((s = responseBuffer.readLine()) != null) {
                output += s;
            }
            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }

    //http:put方法
    public static String sendHttpPut(String url, Map map, String token) throws IOException {
        String output = "";
        try {
            URL targetUrl = new URL(url);
            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("PUT");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            if (token != null) {
                String temp = "Bearer " + token;
                httpConnection.setRequestProperty("Authorization", temp);
            }
            Gson gson = new Gson();
            String param = gson.toJson(map).toString();
            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(param.getBytes());
            outputStream.flush();
            if (httpConnection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : "
                        + httpConnection.getResponseCode());
                return "false";
            }
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));
            String s = "";
            while ((s = responseBuffer.readLine()) != null) {
                output += s;
            }
            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }

    //http:delete方法
    public static String sendHttpDelete(String url, String token) throws IOException {
        String output = "";
        try {
            URL targetUrl = new URL(url);
            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("DELETE");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            if (token != null) {
                String temp = "Bearer " + token;
                httpConnection.setRequestProperty("Authorization", temp);
            }
            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.flush();
            if (httpConnection.getResponseCode() != 200) {
                logger.error("Failed : HTTP error code : "
                        + httpConnection.getResponseCode());
                return "false";
            }
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));
            String s = "";
            while ((s = responseBuffer.readLine()) != null) {
                output += s;
            }
            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }

}